package fr.umlv.m2.recipes.ideas;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;

/**
 * Create the RecipesWidget Activity view in the tab widget
 * @author Christophe Chillan
 *
 */
public class RecipesWidget extends Activity {

	private ListView maListViewPerso;

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView textview = new TextView(this);
		textview.setText("Vous etes dans la section liste des recettes");
		setContentView(R.layout.recipewidget);
		createRecipeListView();
	}


	private void createRecipeListView(){
		//Récupération de la listview créée dans le fichier main.xml
		maListViewPerso = (ListView) findViewById(R.id.listviewperso);

		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		
		Bundle extras = getIntent().getExtras(); 
	    if(extras !=null)
	    {
	    	@SuppressWarnings("unchecked")
			final ArrayList<RecipeBean> recipes = (ArrayList<RecipeBean>) extras.getSerializable("recipes");
			for(RecipeBean recipe : recipes){
				map = new HashMap<String, String>();
				map.put("titre", recipe.getName());
				map.put("description", recipe.getName());
				map.put("img",String.valueOf(getResources().getIdentifier(recipe.getPictureLink()+"_liste", null, getPackageName())));
				listItem.add(map);
			}

			//Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue affichageitem
			SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.recipeslist,
					new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
			maListViewPerso.setAdapter(mSchedule);   


			maListViewPerso.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> a, View v, int position, long id) {

					RecipeBean recipe = recipes.get(position);
					Bundle bundle = new Bundle();
					bundle.putSerializable("recipe", recipe);

					Intent intent = new Intent(RecipesWidget.this.getApplicationContext(), MenuTabIngredient.class);
					intent.putExtras(bundle);
					startActivity(intent);

				}
			});
	    }
		
	}
	/**
	 * @see FavoriteWidget
	 * @param target
	 */
	public void sortAlpha(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getAllRecipesOrderedByName(RecipesDB.getDatabase());
		makeSortedButton(recipes);
		
	}
	
	/**
	 * @see FavoriteWidget
	 * @param target
	 */
	public void sortDifficulty(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getAllRecipesOrderedByDifficulty(RecipesDB.getDatabase());
		makeSortedButton(recipes);
		
	}
	
	/**
	 * @see FavoriteWidget
	 * @param target
	 */
	public void sortPrice(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getAllRecipesOrderedByPrice(RecipesDB.getDatabase());
		makeSortedButton(recipes);
	}
	
	/**
	 * @see FavoriteWidget
	 * @param target
	 */
	public void sortVote(View target){
		List<RecipeBean> recipes = RecipesDB.getRecipeDao().getAllRecipesOrderedByVote(RecipesDB.getDatabase());
		makeSortedButton(recipes);
	}
	
	
	private void makeSortedButton(List<RecipeBean> recipes){
		recipes = new ArrayList<RecipeBean>(recipes);
		Bundle bundle = getIntent().getExtras();
		bundle.putSerializable("recipes", new ArrayList<RecipeBean>(recipes));
		Intent intent = new Intent(getApplicationContext(), MenuTabWidget.class);
		intent.putExtras(bundle);
		startActivity(intent);	
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}
}
